
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class ZtbAppBar extends StatefulWidget implements PreferredSizeWidget{

  const ZtbAppBar({
    Key? key,
    this.backgroundColor: Colors.white,
    this.title: "",
    this.centerTitle: "",
    this.actionName: "",
    this.actionNameColor,
    this.backImg: "assets/images/ic_back_black.png",
    this.onPressed,
    this.backName:"",
    this.isBack: true
  }): super(key: key);

  final Color backgroundColor;
  final String title;
  final String centerTitle;
  final String backImg;
  final String actionName;
  final Color? actionNameColor;
  final VoidCallback? onPressed;
  final bool isBack;
  final String backName;

  @override
  _ZtbAppBarState createState() => _ZtbAppBarState();

  @override
  Size get preferredSize => Size.fromHeight(60.0);
}

class _ZtbAppBarState extends State<ZtbAppBar> {

  SystemUiOverlayStyle _overlayStyle = SystemUiOverlayStyle.light;

  @override
  void initState() {
    super.initState();
    setState(() {
      _overlayStyle = ThemeData.estimateBrightnessForColor(widget.backgroundColor) == Brightness.dark
          ? SystemUiOverlayStyle.light
          : SystemUiOverlayStyle.dark;
    });
  }

  Color getColor(){
    return _overlayStyle == SystemUiOverlayStyle.light ? Colors.white : Color(0xFF333333);
  }

  @override
  Widget build(BuildContext context) {
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: _overlayStyle,
      child: Material(
//        elevation: 2,
        color: widget.backgroundColor,
        child: SafeArea(
          child: Stack(
            alignment: Alignment.centerLeft,
            children: <Widget>[
              Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Container(
                    alignment: widget.centerTitle.isEmpty ? Alignment.centerLeft : Alignment.center,
                    width: double.infinity,
                    child: Text(
                        widget.title.isEmpty ? widget.centerTitle : widget.title,
                      style: TextStyle(fontSize:  widget.title.isEmpty?20:22,fontWeight: widget.title.isEmpty?FontWeight.w400:FontWeight.w500),
                    ),
                    padding: const EdgeInsets.symmetric(horizontal: 20.0),
                  )
                ],
              ),
              widget.isBack ?widget.backName==''? IconButton(
                onPressed: (){
                  Navigator.maybePop(context);
                },
                padding: const EdgeInsets.all(12.0),
                icon: Icon(Icons.arrow_back_ios),
              ):GestureDetector(
                child: Row(
                  children: <Widget>[
                    Icon(Icons.arrow_back_ios),
                    Text(widget.backName,style: TextStyle(fontSize: 18),)
                  ],
                ),onTap: (){
                Navigator.pop(context);
              },
              ) : SizedBox(),
              Positioned(
                right: 0.0,
                child: Theme(
                  data: ThemeData(
                      buttonTheme: ButtonThemeData(
                        padding: const EdgeInsets.symmetric(horizontal: 16.0),
                        minWidth: 60.0,
                      )
                  ),
                  child: widget.actionName.isEmpty ? Container() :
                  FlatButton(
                    child: Text(widget.actionName),
                    textColor: widget.actionNameColor!=null?widget.actionNameColor:getColor(),
                    highlightColor: Colors.transparent,
                    onPressed: widget.onPressed,
                  ),
                ),
              ),
//            IconButton(
//              icon: Icon(Icons.add),
//              highlightColor: Colors.transparent,
//              onPressed: widget.onPressed,
//            )
            ],
          ),
        ),
      ),
    );
  }
}
